import cart from './cart.js'
import address from './address.js'

import {
  getOrder
} from '../../api/orderApi.js'

import {
  addOrder
} from '../../api/orderApi.js'

import router from '../../router/index.js'

export default {
  namespaced: true,
  state: {
    order_list: [],
    // 订单状态
    order_status: {
      '0': '已下单',
      '2': '已付款',
      '4': '已配货',
      '6': '已发货',
      '8': '已收货',
      '10': '已评价',
      '30': '退款中',
      '32': '已退款',
      '34': '退款失败',
      '40': '已取消'
    }
  },
  getters: {},
  mutations: {
    set_order_list(state, val) {
      state.order_list = val;
    }
  },
  actions: {
    //获取订单数据
    get_order_list(context) {
      getOrder().then(function(response) {
        if (response.data.httpcode == 200) {
          context.commit('set_order_list', response.data.data);
        }
      })
    },
    //添加订单
    add_order(context) {
      //提交数据
      let order_submit = {}
      //订单信息
      let order = {
        //订购商品总金额	浮点数	3499.00
        order_spuamount: cart.state.total,
        //订单配送费 浮点数 0
        order_expressfee: 0,
        //订单总金额 浮点数 3499.00
        order_totalamount: cart.state.total,
        // 折扣金额 浮点数 0
        order_discountamount: 0,
        //实付金额
        order_payamount: cart.state.total
      };

      //地址信息
      let addr = {
        //收货人姓名	文本	赵六
        'orderAddress.consignee': address.state.default_address.uaddr_name,
        //收货人电话	文本	13600000002
        'orderAddress.phone': address.state.default_address.uaddr_phone,
        //收货一级行政地区	文本	上海市
        'orderAddress.province': address.state.default_address.uaddr_province,
        //收货二级行政地区	文本	黄浦区
        'orderAddress.city': address.state.default_address.uaddr_city,
        //收货三级行政地区	文本	市区
        'orderAddress.district': address.state.default_address.uaddr_district,
        // 收货详细地址	文本	云南北路59号1401/
        'orderAddress.address': address.state.default_address.uaddr_address
      }
      // 购物车
      let orderDetailList = []
      let index = 0;
      //遍历购物车(cart_checked == 1)
      cart.state.cart_list.forEach(function(c) {
        //只取选中的商品
        if (c.cart_checked == 1) {
          let orderDetail = `{
            "orderDetailList[${index}].odtails_name": "${c.cart_name}",
            "orderDetailList[${index}].odtails_thumburl": "${c.cart_thumburl}",
            "orderDetailList[${index}].odtails_price": ${c.cart_price},
            "orderDetailList[${index}].odtails_count": ${c.cart_count},
            "orderDetailList[${index}].odtails_amount": ${c.cart_count * c.cart_price},
            "orderDetailList[${index}].odtails_scorestatus": 0,
            "orderDetailList[${index}].odtails_spu_id": ${c.cart_spuid},
            "orderDetailList[${index}].odtails_sku": ${JSON.stringify(c.cart_sku)}
          }`;
          index++;
          orderDetailList.push(JSON.parse(orderDetail));
        }
      })
      //遍历数组
      orderDetailList.forEach(function(o) {
        for (let key in o) {
          order_submit[key] = o[key];
        }
      })

      // cart.actions.del_cart('模块之间调用')
      /* context.dispatch('cart/del_cart', '模块之间调用', {
        root: true
      }); */
      //生成订单
      addOrder({
        ...order,
        ...addr,
        ...order_submit
      }).then(function(response) {
        if (response.data.httpcode == 200) {
          alert(`订单创建成功！订单号为: ${response.data.data.order_no}`);
          //删除购物车数据
          cart.state.cart_list.forEach(function(c) {
            if (c.cart_checked == 1) {
              context.dispatch('cart/del_cart', c, {
                root: true
              })
            }
          })
          //加载订单数据
          router.push({
            path: "/orderlist"
          })
        }
      })

    }
  }
}
